// pages/historical_contrast/historical_contrast.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contrastArr: [
    ],
    chooseId: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var hzid = options.hzid
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.setData({
      backgroundColor: bgc,
      hzid: hzid
    })

    var contrastArr = that.data.contrastArr
    for (var i = 0; i < contrastArr.length; i++) {
      contrastArr[i].checked = false
    }
    that.setData({
      contrastArr: contrastArr
    })
  },
  //选择对比
  chooseContrast: function (e) {
    var that = this
    var contrastArr = that.data.contrastArr
    var chooseId = that.data.chooseId
    var bg_id = e.currentTarget.dataset.bg_id
    var index = e.currentTarget.dataset.index
    if (contrastArr[index].checked) {
      chooseId.splice(that.getSubscript(chooseId, bg_id), 1)
    } else {
      chooseId.push(bg_id)
    }
    contrastArr[index].checked = !contrastArr[index].checked
    console.log(chooseId, contrastArr)
    that.setData({
      chooseId: chooseId,
      contrastArr: contrastArr
    })
  },
  //获取下标
  getSubscript: function (arr, str) {
    for (var i = 0, len = arr.length; i < len; i++) {
      if (arr[i] == str) {
        return i
      }
    }
  },
  //对比结果
  contrastResults: function () {
    var that = this
    var chooseId = that.data.chooseId
    var str = JSON.stringify(chooseId)
    if (chooseId.length < 2) {
      wx.showToast({
        title: '至少选择两项',
      })
    } else {
      wx.navigateTo({
        url: '/hyb_yl/mysubpages/pages/contrast_results/contrast_results?chooseId=' + str,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getAllhosbaogao()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getAllhosbaogao: function () {
    var that = this
    var hzid = that.data.hzid
    app.util.request({
      'url': 'entry/wxapp/Alltijianbaogao',
      data: {
        useropenid: wx.getStorageSync('openid'),
        hzid: hzid
      },
      success: function (res) {
        console.log(res)
        var reportObj = res.data.data
        that.setData({
          contrastArr: reportObj
        })
      }
    })
  }
})